#include "Scuba.h"
#include "HTMLizer.h"

#include "ArgumentParser.h"
#include "Helper.h"

#include <string>
#include <iostream>
#include <stdio.h>

using namespace std;

CArgumentParser* gpxArgumentParser;
string gsTarget = "./test.c";

Scuba xScubaDiver("");
HTMLizer xHTMLizer;

int parseCommandLine(int argc, char**argv)
{
	gpxArgumentParser=new CArgumentParser("uh");

	gpxArgumentParser->m_Helper->fillProgramName("nPeriscope");
	gpxArgumentParser->m_Helper->fillAuthor("sonofdelphi");
	gpxArgumentParser->m_Helper->fillSummary("Code To HTML converter");
	gpxArgumentParser->m_Helper->fillUsageLine("[OPTIONS] <SourceFile>");
	gpxArgumentParser->fillHelpForOption('u',"UBoat style output");
	gpxArgumentParser->fillHelpForOption('h',"Displays this help message");

	gpxArgumentParser->m_Helper->fillYear("2009");
	gpxArgumentParser->m_Helper->fillOwner("The Oraclique");
	gpxArgumentParser->m_Helper->fillTailMessage("Part of Neptuner Code-Management Suite.\nBuild:" __DATE__"/"__TIME__  "\nThis program comes with absolutely no warranty.");

	int nParseEndPos = gpxArgumentParser->parse(argc,argv);
	
	if(!gpxArgumentParser->wasParseSuccessful())
	{
		gpxArgumentParser->m_Helper->displayHelp();
		delete gpxArgumentParser;
		cerr<<"Invalid option supplied"<<endl;
		return -1;
	}
	
	return nParseEndPos+1;
}

int main(int argc, char**argv)
{
	int nProgArgIndex = parseCommandLine(argc, argv);
	if(nProgArgIndex<0)
	{
		return 1;
	}

	if(gpxArgumentParser->wasOptionSupplied('h'))
	{
		gpxArgumentParser->m_Helper->displayHelp();
		delete gpxArgumentParser;
		return 0;
	}

	if(nProgArgIndex<argc)
	{
			gsTarget=argv[nProgArgIndex];
	}
	else
	{
		cerr<<"Attempting to proceed with default test file: "<<gsTarget<<endl;
	}

	if(gpxArgumentParser->wasOptionSupplied('u'))
		xHTMLizer.m_bUBoatOutput = true;

	xScubaDiver.dive(gsTarget);

	// We need to gather some basic stats before parse, to generate headers
	xHTMLizer.m_nLines = xScubaDiver.getNumberOfLines();
	xHTMLizer.m_sFilename = xScubaDiver.m_sFilename;
	xHTMLizer.generateHeader();

	xScubaDiver.walk(xHTMLizer);
	xHTMLizer.generateFooter();

	if(argc==1)
	{
		cerr<<"Press ENTER to continue..";
		getchar();
	}

    return 0;
}


